﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    DialogLauncherWindow.xaml.cs
//
// Abstract:
//    
//    Implements a WPF window to host the Notification Rules Property control
//
//--

using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace Microsoft.EnhancedReminders.OutlookAddIn
{
    /// <summary>
    /// Interaction logic for DialogLauncherWindow.xaml
    /// </summary>
    public partial class DialogLauncherWindow : Window
    {
        /// <summary>
        /// Command for "OK" button events.
        /// </summary>
        public readonly static RoutedCommand OkCommand = new RoutedCommand();

        /// <summary>
        /// Command for "Apply" button events.
        /// </summary>
        public readonly static RoutedCommand ApplyCommand = new RoutedCommand();
        private bool _isDirty;

        /// <summary>
        /// ctor.
        /// </summary>
        public DialogLauncherWindow()
        {
            InitializeComponent();

            Background = notificationRulesPropertyPage.rulesButton.Background;
            notificationRulesPropertyPage.channelControl.PropertyChanged += ChannelControlPropertyChanged;
        }

        /// <summary>
        /// Returns whether or not the data is dirty; this determines whether the apply button
        /// should be enabled or not.
        /// </summary>
        private void IsDirty(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _isDirty;
        }

        /// <summary>
        /// Updates the dirty flag when the Channel Control changes.
        /// </summary>
        private void ChannelControlPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            _isDirty = true;
        }

        /// <summary>
        /// Apply changes to the Channels Control and close the dialog.
        /// </summary>
        private void Ok(object sender, ExecutedRoutedEventArgs e)
        {
            Apply(sender, e);
            DialogResult = true;
            Close();
        }

        /// <summary>
        /// Apply changes to the Channels Control
        /// </summary>
        private void Apply(object sender, ExecutedRoutedEventArgs e)
        {
            _isDirty = false;
            notificationRulesPropertyPage.SaveChannels();
        }
    }
}
